#pragma  once
#include "stdafx.h"
#include "units.h"
#include "automatic_item.h"
class QGraphicsSceneMouseEvent;
class SceneView;
class Common;
class ConnectionPoint;
class Connector;
class Title;
class ParametersModel;
class IndicatorItem;
class IndicatorLine;
//class Units;
class automaticItem;

class Indicator : public QGraphicsObject
{
	Q_OBJECT

	
public:
	
	Indicator(Common* _common, SceneView* _scene, automaticItem* _parent,
		const QPointF &_position_relative_source, const bool &_visible, const QStringList &_parameters = QStringList());
	~Indicator();

	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust(const QPointF &_pos);
	void setIndicatorVisible(const bool &_visible);
	bool lineIsVisible();
	//void setLineVisible(const bool &_visible){ line.data()->setVisible(_visible); }
	void setLineVisible(bool _visible);
	QString getAllParameters();
	void setAllParameters(const QString &_data);
	void setPos(const QPointF &_pos){ setX(_pos.x()); setY(_pos.y()); update_position_relative_source();  }
	void updateParameters(const QStringList &_parameters, const QStringList &_units, const QVector<int> &_digits);
	void setParameterValue(const QString _parameter, const double &_value, const int &_digits);
	automaticItem* getParent(){ return parent; }
private:
	Common* common;
	SceneView* scene;	
	automaticItem* parent;
	QRectF size;
	QPointF position_relative_source;
	QGraphicsLineItem* line;
	hash_map<string, IndicatorItem*> list_of_parameters;
	int countDisplayedParameters();
	void updateItemPosition();
	void updateLinePosition();
	double getDistance(const QPointF &_p1, const QPointF &_p2);
	void addParameters(const QStringList &_parameters);
	void setEditMode(const bool &_b);
	bool editmode;
	void update_position_relative_source();
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private slots:
	void trend(const QString &_parameter);
signals:
	void createTrend(const QString &_parameter);
};

//=========================================================================================================================

class IndicatorItem : public QGraphicsObject{
	Q_OBJECT
public:
	IndicatorItem(SceneView* _scene, Indicator *_indicator, const QString &_full_name, const QString &_short_name, const double &_value, const QString &_unit, const bool &_display);
	~IndicatorItem();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust(const QPointF &_pos);
	
	QString short_name;//private
	bool display;
	QString unit;
	void setEditMode(const bool &_b){ editmode = _b; this->setVisible(editmode || display); update();}
	void setValue(const double &_value, const int &_digits);
	void setUnit(const QString &_unit);
	void setDigit(const int &_digits){ digits = _digits;};
	void setIndicatorItemVisible(const bool &_visible);
private:
	SceneView* scene;
	Indicator *indicator;
	bool editmode;
	QString full_name;
	int digits;
	double value;
	
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private slots:
	void trend();
	void add_to_trend_list();
signals:
	void createTrend(const QString &_parameter);
};
